# clear working environment
rm(list = ls())

library(tidyverse)
library(janitor)
library(kableExtra)

# load data
df <- read_rds("data/dem-transitions-replication-data.rds") %>%
  glimpse()

# create lagged dem, transition indicator, lead variables
df <- df %>%
  mutate(dem = ifelse(v2x_regime >= 2, 1, 0)) %>%
  dplyr::group_by(country) %>%
  mutate(dem_lag_1 = dplyr::lag(dem)) %>%
  mutate(regime_lag_1 = dplyr::lag(v2x_regime)) %>%
  mutate(transition = ifelse(dem == 1 & dem_lag_1 == 0, 1, 0)) %>%
  mutate(dem_lead_1 = dplyr::lead(dem)) %>%
  mutate(dem_lead_2 = dplyr::lead(dem, n = 2)) %>%
  mutate(dem_lead_3 = dplyr::lead(dem, n = 3)) %>%
  mutate(dem_lead_4 = dplyr::lead(dem, n = 4)) %>%
  mutate(dem_lead_5 = dplyr::lead(dem, n = 5)) %>%
  mutate(regime_lead_1 = dplyr::lead(v2x_regime)) %>%
  mutate(regime_lead_2 = dplyr::lead(v2x_regime, n = 2)) %>%
  mutate(regime_lead_3 = dplyr::lead(v2x_regime, n = 3)) %>%
  mutate(regime_lead_4 = dplyr::lead(v2x_regime, n = 4)) %>%
  mutate(regime_lead_5 = dplyr::lead(v2x_regime, n = 5)) %>%
  mutate(constraints_lag_1 = dplyr::lag(v2xlg_legcon)) %>%
  mutate(civil_lag_1 = dplyr::lag(v2x_cspart)) %>%
  mutate(free_express_lag_1 = dplyr::lag(v2x_freexp_altinf)) %>%
  mutate(lag_transition = as.factor(dplyr::lag(transition))) %>%
 glimpse()

#### make Table B.1 for the Appendix ####
# keep only the transitioned cases 
t_df <- df %>%
  filter(transition == 1) %>%
  ungroup() %>%
  glimpse()

# drop right censored cases to calculate probability of no reversal
t_df1 <- t_df %>%
  filter(dem_lead_1 >= 0 & dem_lead_2 >= 0 & dem_lead_3 >= 0 & dem_lead_4 >= 0 & dem_lead_5 >= 0) %>%
  glimpse()

# table of regime transition types
t_df <- t_df %>%
  mutate(before = ifelse(regime_lag_1 == 0, "Closed Autocracy", "Electoral Autocracy")) %>%
  mutate(after = ifelse(v2x_regime == 2, "Electoral Democracy", "Liberal Democracy")) %>%
  glimpse()

transition_types <- tabyl(t_df, before, after) %>%
  adorn_totals(c("row", "col")) %>%
  adorn_percentages("row") %>% 
  adorn_pct_formatting(digits = 0) %>%
  adorn_ns(position = "front") %>%
  adorn_title("combined")
names(transition_types)[1] = c("Before Transition")

kbl(transition_types, align = c("l", rep("r", times = ncol(transition_types)-1)), booktabs = T, format = "latex",
    caption = "Distribution of Democratic Transitions by Regime Type") %>%
  add_header_above(c("", "After Transition" = 2, "")) %>%
  kable_styling(full_width = FALSE)

#### what is the probability of authoritarian reversal ####
# calculate the probability of NO reversal in the next 1, 2, 3, 4, 5 years
t_df1 <- t_df1 %>%
  mutate(no_rev_1 = ifelse(dem_lead_1 == 1, 1, 0)) %>%
  mutate(no_rev_2 = ifelse(dem_lead_1 == 1 & dem_lead_2 == 1, 1, 0)) %>%
  mutate(no_rev_3 = ifelse(dem_lead_1 == 1 & dem_lead_2 == 1 & dem_lead_3 == 1, 1, 0)) %>%
  mutate(no_rev_4 = ifelse(dem_lead_1 == 1 & dem_lead_2 == 1 & dem_lead_3 == 1 & dem_lead_4 == 1, 1, 0)) %>%
  mutate(no_rev_5 = ifelse(dem_lead_1 == 1 & dem_lead_2 == 1 & dem_lead_3 == 1 & dem_lead_4 == 1 & dem_lead_5 == 1, 1, 0)) %>%
  glimpse()

# confidence intervals around these estimates
one_yr <- prop.test(x = sum(t_df1$no_rev_1), n = length(t_df1$no_rev_1))
round(one_yr[["estimate"]], 2)
round(one_yr[["conf.int"]], 2)
two_yr <- prop.test(x = sum(t_df1$no_rev_2), n = length(t_df1$no_rev_2))
round(two_yr[["estimate"]], 2)
round(two_yr[["conf.int"]], 2)
three_yr <- prop.test(x = sum(t_df1$no_rev_3), n = length(t_df1$no_rev_3))
round(three_yr[["estimate"]], 2)
round(three_yr[["conf.int"]], 2)
four_yr <- prop.test(x = sum(t_df1$no_rev_4), n = length(t_df1$no_rev_4))
round(four_yr[["estimate"]], 2)
round(four_yr[["conf.int"]], 2)
five_yr <- prop.test(x = sum(t_df1$no_rev_5), n = length(t_df1$no_rev_5))
round(five_yr[["estimate"]], 2)
round(five_yr[["conf.int"]], 2)

#### what is the level of constraints at the time of democratic transitions compared to democratic and autocratic median ####
# executive constraints over time by regime type
constraints_gg <- df %>%
  dplyr::group_by(dem, year) %>%
  dplyr::summarize(median_constraints = median(constraints_lag_1, na.rm = TRUE)) %>%
  filter(dem >= 0) %>%
  mutate(regime_type = case_when(dem == 0 ~ "Autocratic Median",
                                 dem == 1 ~ "Democratic Median")) %>%
  glimpse()

# create arrows to highlight Argentina (1964) transition as an example of a transition
arrows <- 
  tibble(x1 = c(1972), x2 = c(1963.78), y1 = c(.32), y2 = c(.6725))

# create Figure 3 for the main text
ggplot() +
  geom_point(data = t_df, aes(x = year, y = constraints_lag_1), alpha = 0.25) +
  geom_line(data = constraints_gg, aes(x = year, y = median_constraints, color = regime_type), linewidth = 0.85) +
  geom_vline(xintercept = (quantile(t_df$year, probs = .25)-0.5), linetype = "dashed") +
  annotate("text", x = 1920, y = .07, label = "Gray points represent instances of democratic transitions.", size = 2.25) +
  annotate("text", x = 1980, y = .32, label = "Argentina (1964)", size = 3) +
  geom_curve(
    data = arrows, aes(x = x1, y = y1, xend = x2, yend = y2),
    arrow = arrow(length = unit(0.08, "inch")), linewidth = 0.25,
    color = "gray20", curvature = -0.4) +
  ylim(0,1) +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgray")) +
  labs(x = "Year",
       y = "Executive Constraints (V-Dem)",
       color = "")

# save plot
ggsave("figures/executive-constraints.pdf", width = 7.5, height = 4.5, units = "in", bg = "white")

# calculate how many transitioning countries are at or above the median executive constraints for autocracies
constraints_gg <- constraints_gg %>%
  filter(dem == 0) %>%
  ungroup() %>%
  select(year, median_constraints) %>%
  glimpse()

t_df <- left_join(t_df, constraints_gg, by = "year") %>%
  glimpse()

t_df <- t_df %>%
  mutate(high_constraint = ifelse(constraints_lag_1 >= median_constraints, 1, 0)) %>%
  glimpse()

round(mean(t_df$high_constraint, na.rm = TRUE), 2)

#### what is the level of free expression at the time of democratic transitions compared to democratic and autocratic median ####
# freedom of expression over time by regime type
free_gg <- df %>%
  dplyr::group_by(dem, year) %>%
  dplyr::summarize(median_free = median(free_express_lag_1, na.rm = TRUE)) %>%
  filter(dem >= 0) %>%
  mutate(regime_type = case_when(dem == 0 ~ "Autocratic Median",
                                 dem == 1 ~ "Democratic Median")) %>%
  glimpse()

# create Figure B.1 for the Appendix
ggplot() +
  geom_point(data = t_df, aes(x = year, y = free_express_lag_1), alpha = 0.25) +
  geom_line(data = free_gg, aes(x = year, y = median_free, color = regime_type), linewidth = 0.85) +
  geom_vline(xintercept = (quantile(t_df$year, probs = .25)-0.5), linetype = "dashed") +
  theme_minimal()+
  theme(legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgray")) +
  labs(x = "Year",
       y = "Freedom of Expression (V-Dem)",
       color = "")
ggsave("figures/freedom-expression.pdf", width = 7.5, height = 4.5, units = "in", bg = "white")

# calculate how many transitioning countries are at or above the median freedom of expression for autocracies
free_gg <- free_gg %>%
  filter(dem == 0) %>%
  ungroup() %>%
  select(year, median_free) %>%
  glimpse()

t_df <- left_join(t_df, free_gg, by = "year") %>%
  glimpse()

t_df <- t_df %>%
  mutate(high_free = ifelse(free_express_lag_1 >= median_free, 1, 0)) %>%
  glimpse()

round(mean(t_df$high_free, na.rm = TRUE), 2)

#### what is the level of civil society at the time of democratic transitions compared to democratic and autocratic median ####
# civil society over time by regime type
civil_gg <- df %>%
  dplyr::group_by(dem, year) %>%
  dplyr::summarize(median_civil = median(civil_lag_1, na.rm = TRUE)) %>%
  filter(dem >= 0) %>%
  mutate(regime_type = case_when(dem == 0 ~ "Autocratic Median",
                                 dem == 1 ~ "Democratic Median")) %>%
  glimpse()

# create Figure B.2 for the Appendix
ggplot() +
  geom_point(data = t_df, aes(x = year, y = civil_lag_1), alpha = 0.25) +
  geom_line(data = civil_gg, aes(x = year, y = median_civil, color = regime_type), linewidth = 0.85) +
  geom_vline(xintercept = (quantile(t_df$year, probs = .25)-0.5), linetype = "dashed") +
  ylim(0, 1) +
  theme_minimal()+
  theme(legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgray")) +
  labs(x = "Year",
       y = "Civil Society Participation (V-Dem)",
       color = "Regime Type")

# save plot
ggsave("figures/civil-society.pdf", width = 7.5, height = 4.5, units = "in", bg = "white")

# calculate how many transitioning countries are at or above the median civil society participation for autocracies
civil_gg <- civil_gg %>%
  filter(dem == 0) %>%
  ungroup() %>%
  select(year, median_civil) %>%
  glimpse()

t_df <- left_join(t_df, civil_gg, by = "year") %>%
  glimpse()

t_df <- t_df %>%
  mutate(high_civil = ifelse(civil_lag_1 >= median_civil, 1, 0)) %>%
  glimpse()

round(mean(t_df$high_civil, na.rm = TRUE), 2)


